//
//  RouteCell.m
//  RRQS
//
//  Created by kuyuZJ on 16/8/17.
//
//

#import "RouteCell.h"

@implementation RouteCollectionViewInfo

@end

#define RouteCellLeftMargin     10
#define RouteCellTopMargin      3

@interface RouteCell ()
{
    UILabel *_titleLabel;
    UILabel *_subtitleLabel;
    UIImageView *_prevImageView;
    UIImageView *_nextImageView;
}

@property(nonatomic,strong) UILabel* labelTitle;
@property(nonatomic,strong) UILabel* labelSubTitle;
@property(nonatomic,strong) UIImageView* prevIcon;
@property(nonatomic,strong) UIImageView* nexIcon;

@end

@implementation RouteCell


#pragma mark - Life Cycle

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
        self.contentView.clipsToBounds = YES;
        
        /*_prevImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"leftArrow"]];
         _prevImageView.center = CGPointMake(RouteCellLeftMargin + CGRectGetWidth(_prevImageView.bounds) / 2.0, CGRectGetHeight(frame) / 2.0);
         _prevImageView.hidden = YES;
         [self.contentView addSubview:_prevImageView];
         
         _nextImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rightArrow"]];
         _nextImageView.center = CGPointMake(CGRectGetWidth(frame) - RouteCellLeftMargin - CGRectGetWidth(_nextImageView.bounds) / 2.0, CGRectGetHeight(frame) / 2.0);
         _nextImageView.hidden = YES;
         [self.contentView addSubview:_nextImageView];
         
         CGFloat labelWidth = CGRectGetWidth(frame) - CGRectGetWidth(_prevImageView.bounds) * 2 - RouteCellLeftMargin * 4;
         
         _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(RouteCellLeftMargin + RouteCellLeftMargin + CGRectGetWidth(_prevImageView.frame), RouteCellTopMargin, labelWidth, 32)];
         _titleLabel.numberOfLines = 2;
         _titleLabel.font = [UIFont boldSystemFontOfSize:14];
         [self.contentView addSubview:_titleLabel];
         
         _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(RouteCellLeftMargin + RouteCellLeftMargin + CGRectGetWidth(_prevImageView.frame), CGRectGetMaxY(_titleLabel.frame), labelWidth, 20)];
         _subtitleLabel.font = [UIFont systemFontOfSize:12];
         [self.contentView addSubview:_subtitleLabel];
         */
        [self layoutUI];
        [self layoutConstraints];
    }
    return self;
}


#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    [self addSubview:self.labelTitle];
    [self addSubview:self.labelSubTitle];
    [self addSubview:self.prevIcon];
    [self addSubview:self.nexIcon];
}

-(void)layoutConstraints{
    NSArray* formats = @[  @"H:|-5-[prevIcon(==iconWidth)]",
                           @"V:[prevIcon(==iconHeight)]",
                           @"H:[nextIcon(==iconWidth)]-5-|",
                           @"V:[nextIcon(==iconHeight)]",
                           @"H:[prevIcon]-5-[labelTitle]-5-[nextIcon]",
                           @"V:|-[labelTitle(==titleHeight)]",
                           @"H:[prevIcon]-5-[labelSubTitle]-5-[nextIcon]",
                           @"V:[labelTitle][labelSubTitle]-0-|"
                           ];
    NSDictionary* metorics = @{ @"iconWidth":@(10), @"iconHeight":@(18), @"titleHeight":@(32)};
    NSDictionary* vies = @{ @"prevIcon":self.prevIcon, @"nextIcon":self.nexIcon, @"labelTitle":self.labelTitle, @"labelSubTitle":self.labelSubTitle};
    for (NSString* format in formats) {
        NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metorics views:vies];
        [self addConstraints:constraints];
    }
    
    NSLayoutConstraint* constraint = [NSLayoutConstraint constraintWithItem:self.prevIcon attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f];
    [self addConstraint:constraint];
    constraint = [NSLayoutConstraint constraintWithItem:self.nexIcon attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f];
    [self addConstraint:constraint];
    
}

#pragma mark - Interface

- (void)setInfo:(RouteCollectionViewInfo *)info
{
    _info = info;
    
    self.labelTitle.text = _info.title;
    self.labelSubTitle.text = _info.subtitle;
}

- (void)setShouldShowPrevIndicator:(BOOL)shouldShowPrevIndicator
{
    _shouldShowPrevIndicator = shouldShowPrevIndicator;
    self.prevIcon.hidden = !self.shouldShowPrevIndicator;
}

- (void)setShouldShowNextIndicator:(BOOL)shouldShowNextIndicator
{
    _shouldShowNextIndicator = shouldShowNextIndicator;
    self.nexIcon.hidden = !self.shouldShowNextIndicator;
}

#pragma mark =====================================================  property package

-(UILabel *)labelTitle{
    if(!_labelTitle){
        _labelTitle = [[UILabel alloc]init];
        _labelTitle.numberOfLines = 2;
        _labelTitle.font = [UIFont boldSystemFontOfSize:14];
        _labelTitle.translatesAutoresizingMaskIntoConstraints  = NO;
    }
    return _labelTitle;
}

-(UILabel *)labelSubTitle{
    if(!_labelSubTitle){
        _labelSubTitle = [[UILabel alloc]init];
        _labelSubTitle.font = [UIFont boldSystemFontOfSize:12];
        _labelSubTitle.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _labelSubTitle;
}

-(UIImageView *)prevIcon{
    if(!_prevIcon){
        _prevIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"leftArrow"]];
        _prevIcon.hidden = YES;
        _prevIcon.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _prevIcon;
}

-(UIImageView *)nexIcon{
    if(!_nexIcon){
        _nexIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"rightArrow"]];
        _nexIcon.hidden = YES;
        _nexIcon.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _nexIcon;
}


@end
